<?php
/**
 * Created by PhpStorm.
 * User: mr.lee
 * Date: 2018/3/28
 * Time: 上午8:56
 * 卡券投放操作
 */

namespace app\common\controller;


use app\common\model\CardGivecardLog;

use app\common\model\CardMember;
use app\common\model\CardUserGet;
use app\common\model\Merchant;
use app\common\model\Paylog;
use app\common\model\PaymentCardPut;
use app\common\model\PaymentCardPutLog;
use think\Controller;


class AfterPayPutCardTest extends Wxoption
{

    //orderid
    public function paymentcard($orderid){
//        try {
            $Paylog = new Paylog();
            $param = $Paylog->where('orderid',$orderid)->field('orderid,pay_fs,pay_cate,mid,price,sub_openid,add_time')->find();
//                if ($param['pay_fs'] == "0" || $param['pay_fs'] == "2") {
                    $CardUserGet = new CardUserGet();
                    $PaymentCardPut = new PaymentCardPut();
                    $PaymentCardPutLog = new PaymentCardPutLog();
                    $paycardlost = $PaymentCardPutLog->where('orderid', $param['orderid'])->field('id,putstatu')->find();
                    $Merchant = new Merchant();
                    $top_mid = $Merchant->where(['id' => $param['mid']])->value('top_mid');
                    $mab['top_mid'] = $top_mid;
                    $mab['statu'] = 1;
                    $mab['start_time'] = ['lt', time()];
                    $mab['end_time'] = ['gt', time()];
                    $mab['min_fee'] = ['elt', $param['price']];
                    $mab['max_fee'] = ['egt', $param['price']];
                    $payment = $PaymentCardPut->where($mab)->find();

                    //判断是否有支付后送券
                    if ($payment) {
                        //查询领卡记录
                        $map['top_mid'] = $top_mid;
                        $map['card_type'] = 'MEMBER_CARD';
                        $map['openid'] = $param['sub_openid'];
                        $user = $CardUserGet->where($map)->field('card_id,code,id,top_mid,openid,nickname')->find();
                        if ($user) {

                            if ($paycardlost['putstatu'] == 1) {
                                //如果等于1则结束
                                $arrc = 'error2';
                            } else {
                                //如果领卡记录等于0
                                $data['orderid'] = $param['orderid'];
                                $data['card_id'] = $user['card_id'];
                                $data['code'] = $user['code'];
                                $data['openid'] = $user['openid'];
                                $data['fid'] = $payment['id'];
                                $data['price'] = $param['price'];
                                $data['paytime'] = $param['add_time'];
                                $data['putstatu'] = 1;
                                $PaymentCardPutLog->allowField(true)->save($data);
                                $give_rec_id = $PaymentCardPutLog->id;
                                //发放卡券
                                $arr['orderid'] = $param['orderid'];
                                $arr['card_id'] = $user['card_id'];
                                $arr['code'] = $user['code'];
                                $arr['openid'] = $user['openid'];
                                $arr['nickname'] = $user['nickname'];
                                $arr['mid'] = $param['mid'];
                                $arr['give_rec_id'] = $give_rec_id;
                                $arr['id'] = $user['id'];
                                $arr['top_mid'] = $user['top_mid'];
                                //余额更新
                                $arrc = $this->paymentputcard($arr, $payment);

                            }
                        }else{
                            $arrc = 'errorn';
                        }
                    } else {
                        $arrc = 'error1';
                    }
//                } else {
//                    $arrc = 'error2';
//                }
            return $arrc;
//        } catch (\Exception $e) {
//            $arrc = 'error' . $e->getMessage();
//            $ActionLogs = new ActionLogs();
//            $ActionLogs->writelog("osapi", "clientlog", "paymentcard", "e:", $arrc);
//        }
    }

    //支付后送券
    public function paymentputcard($param,$payment){
        $Paylog = new Paylog();
        $payinfo = $Paylog->where(['orderid'=>$param['orderid']])->find();
        //是否发送过
        if ($payinfo['pay_is_put'] == 0 || $payinfo['pay_is_put'] == '0'){
            $Merchant = new Merchant();
            $top_mid = $Merchant->where(['id'=>$payinfo['mid']])->value('top_mid');
            $PaymentCardPut = new PaymentCardPut();
            $map['top_mid'] = $top_mid;
            $map['statu'] = 1;
            $CardMember = new CardMember();
            $CardUserGet = new CardUserGet();
            $cardus = $CardMember->where(['top_mid'=>$top_mid])->field('card_id')->find();
            $user = $CardUserGet->where(['card_id'=>$cardus['card_id'],'openid'=>$payinfo['openid']])->field('nickname,code,card_id')->find();
            if ($payment['num'] > $payment['get_num']){
                $card_no = count(json_decode($payment['zs_card_id_all'],true));
                if($card_no > 0){
                    $arr['zs_card_id_all'] = $payment['zs_card_id_all'];
                    $arr['openid'] = $param['openid'];
                    $arr['code'] = $user['code'];
                    $arr['mid'] = $payinfo['mid'];
                    $arr['card_id'] = $cardus['card_id'];
                    $arr['give_rec_id'] = $param['give_rec_id'];
                    $arr['top_mid'] = $top_mid;
                    $res = $this->payment_zs_cardslog($arr);
                    if ($res) {
                        $SendMsg = new TemplateMsgt();
                        $arrs['first'] = '点击“详情”领取优惠券';
                        $arrs['mid'] = $arr['mid'];
                        $arrs['openid'] = $param['openid'];
                        $arrs['card_id'] = '';
                        $arrs['name'] = $user['nickname'] == ''?'您好':$user['nickname'];
                        $arrs['title'] = $payment['zs_card_info'];
                        $arrs['give_id'] = $payment['id'];
                        $arrs['type'] = 'paymentcard';
                        $arrs['url'] = '/mobile.php?s=/cardget/getpaymentcards/mid/' . $arr['mid'] . '/rec_id/' . $arr['give_rec_id'];
                        $resu = $SendMsg->send_tpl('put_gift', $arrs);
                    } else {
                        $resu = '';
                    }
                    $resut['card'] = $resu;
                    $datac['giveinfo'] = json_encode($resut, JSON_UNESCAPED_UNICODE);
                    $datac['paystatu'] = 1;
                    $datac['paytime'] = time();
                    $PaymentCardPutLog = new PaymentCardPutLog();
                    $PaymentCardPutLog->allowField(true)->save($datac,['id'=>$param['give_rec_id']]);
                    $PaymentCardPut->where(['id'=>$payment['id']])->setInc('get_num');
                } else {
                    $resut['card'] = '无券赠送';
                }
            }else{
                $resut = 'error3';
            }

        }else{
            $resut = 'error1';
        }
        return $resut;
    }

    //生成赠卡记录[支付后
    /*
     * zs_card_id_all / card_id  /code /  give_rec_id  / mid / top_mid  / put_type
     * */
    public function payment_zs_cardslog($arr){
        $CardGivecardLog = new CardGivecardLog();
        $zs_card_id = $arr['zs_card_id_all'];
        if ($zs_card_id){
            $zs_card_id = json_decode($arr['zs_card_id_all'],true);

            $data = [];
            foreach ($zs_card_id as $k=>$v) {
                if ($this->fileoffcards($v['zs_card_id']) == true) {
                    $data[$k]['mid'] = $arr['mid'];
                    $data[$k]['card_id'] = $arr['card_id'];
                    $data[$k]['code'] = $arr['code'];
                    $data[$k]['openid'] = $arr['openid'];
                    $data[$k]['zs_card_id'] = $v['zs_card_id'];
                    $data[$k]['give_rec_id'] = $arr['give_rec_id'];
                    $data[$k]['mid'] = $arr['mid'];
                    $data[$k]['top_mid'] = $arr['top_mid'];
                    $data[$k]['put_sume'] = $v['zs_no'];
                    $data[$k]['put_type'] = 'payment_card';
                    $data[$k]['create_time'] = time();
                }
            }
            $res = $CardGivecardLog->saveAll($data);
        }else{
            $res = '';
        }
        return $res;
    }

    public function fileoffcards($card_id)
    {
        $CardGel = new \app\common\model\CardGeneral();
        $card = $CardGel->where('card_id', $card_id)->field('end_timestamp,stocks,consume,time_type')->find();
        if ($card['time_type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
            $time = time();
            if ($card['end_timestamp'] < $time) {
                $res = false;
            } else {
                if ($card['stocks'] > 0) {
                    $res = true;
                } else {
                    $res = false;
                }
            }
        } else {
            $res = true;
        }
        return $res;
    }


}